package impl.repository;

import interfaces.repository.BillingRepository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import dao.AccessDatabase;
import domain.Billing;
import domain.Customer;

public class StubBillingRepositoryImpl implements BillingRepository {

	@Override
	public Billing findById(int idCustomer) {
		Billing billing = null;
		StubAddresRepositoryImpl sari = new StubAddresRepositoryImpl();
		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM billing WHERE idCustomer='"
							+ idCustomer + "'");

			billing = new Billing(rs.getInt("idBilling"), rs.getString("creditCardType"), rs.getString("creditCardNumber"), rs.getDate("expirationDate"), rs.getString("firstName"), rs.getString("lastName"), sari.findById(idCustomer));

			con.close();

			return billing;
		} catch (SQLException e) {
			return null;
		}
	}

	
	@Override
	public boolean edit(Billing before, Billing after) {
		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("UPDATE `billing` SET `credicartype`='"+ after.getCreditCardType() + "', `credicarnumber`='" + after.getCreditCardNumber()	+ "', `expirationDate`='" + after.getExpirationDate()+ "', `firstName`='" + after.getFirstName()+ "', `lastName`='" + after.getLastName()+ "' WHERE `idBilling`='" + before.getId() + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public boolean add(int idClient, Billing billing) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("INSERT INTO `billing` (`idCustomer`,`idAddress`,`credicartype`,`credicarnumber`,`expirationDate`,`firstName`,`lastName`) VALUES ('"
				
					+ idClient+ "','"+ billing.getAddress().getId()+ "','"+ billing.getCreditCardType()+ "','"+ billing.getCreditCardNumber()+ "','"+ billing.getExpirationDate()+ "','"+ billing.getFirstName()+ "','"+ billing.getLastName()+ "');");
			
			con.close();
			return true;
		} catch (SQLException e) {
			System.out
					.println("nao foi possivel prosseguir com o registro" + e);
			return false;
		}

	}

	@Override
	public boolean delete(int billing) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `billing` WHERE `idBilling`='"
					+ billing + "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}
	}
}
